package com.ocom.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.human.EntityRolePermission;
import com.ocom.common.entity.master.EntityCompanyPart;
import com.ocom.common.entity.master.EntityCompanyRole;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.vo.company.CompanyRoleListVo;
import com.ocom.company.dao.IDaoCompanyPart;
import com.ocom.company.dao.IDaoCompanyRole;
import com.ocom.company.service.CompanyPartService;
import com.ocom.company.service.CompanyRoleService;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.redis.aspect.CacheRedisAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.PermissionCollection;
import java.util.Calendar;
import java.util.List;


@Service
public class CompanyRoleImpl extends ServiceImpl<IDaoCompanyRole, EntityCompanyRole> implements CompanyRoleService {


    @Autowired
    private IDaoCompanyRole dao;

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityCompanyRole> getCompanyRoleList(Long companyId) {
        QueryWrapper<EntityCompanyRole> queryWrapper = Wrappers.query();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.orderByAsc("role_id");
        return this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public Boolean addCompanyRole(Long companyId, EntityCompanyRole entityCompanyRole) {
        return this.save(entityCompanyRole);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public Boolean modifyCompanyRole(Long companyId, Long roleId, String roleName) {

        UpdateWrapper<EntityCompanyRole> updateWrapper = Wrappers.update();
        updateWrapper.eq("role_id", roleId);

        updateWrapper.set("role_name", roleName);
        return this.update(updateWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB)
    public Boolean delCompanyRole(Long companyId, Long roleId) {
        QueryWrapper<EntityCompanyRole> queryWrapper = Wrappers.query();
        queryWrapper.eq("role_id", roleId);
        queryWrapper.gt("role_id", 10000L);
        return this.remove(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB, isMaster = MasterSlaveEnum.SLAVE)
    public Integer countCompanyRole(Long companyId, Long roleId, String roleName, int footwork ) {
        QueryWrapper<EntityCompanyRole> queryWrapper = Wrappers.query();
        switch (footwork) {
            case 1 :
                queryWrapper.le("role_id", 10000L);
                break;
            case 2:
                queryWrapper.gt("role_id", 10000L);
                queryWrapper.eq("com_id", companyId);
                break;
            case 3:
                queryWrapper.eq("com_id", companyId);
        }

        queryWrapper.eq("role_name", roleName);
        queryWrapper.last("limit 1");
        return this.count(queryWrapper);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<CompanyRoleListVo> getCompanyRole(Long companyId, Long roleId) {
        return dao.getCompanyRole(companyId, roleId);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB, isMaster = MasterSlaveEnum.SLAVE)
    public EntityCompanyRole getComRole(Long comId, Long roleId) {

        return this.getOne(Wrappers.lambdaQuery(EntityCompanyRole.class).eq(EntityCompanyRole::getComId,comId).eq(EntityCompanyRole::getRoleId,roleId).last("limit 1"));
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.MAIN_DB, isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityCompanyRole> getByParentRole(Integer parentRole) {
        return this.list(Wrappers.lambdaQuery(EntityCompanyRole.class).eq(EntityCompanyRole::getParentRole,parentRole));
    }
}
